package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    
    String s="";
    myCountDownTimer timerGen;
    myCountDownTimer timerProp;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Question q = tirerQuestion();
        String question = "Pourquoi la vie ? (hint: how many roads a man can take in his life?)";
        final String reponse = "42";

        TextView quest = (TextView) findViewById(R.id.question);
        final Button rep = (Button) findViewById(R.id.rep);
        final Button valider = (Button) findViewById(R.id.val);
        Button arreter = (Button) findViewById(R.id.arret);
        final TextView compteurGen = (TextView) findViewById(R.id.compteurGen);
        final TextView compteurProp = (TextView) findViewById(R.id.compteurProp);
        final EditText prop = (EditText) findViewById(R.id.prop);
        final TextView message = (TextView) findViewById(R.id.message);
        
        quest.setText(question);
        
        prop.setVisibility(View.INVISIBLE);
        valider.setVisibility(View.INVISIBLE);
        compteurProp.setVisibility(View.INVISIBLE);
              
        timerGen = new myCountDownTimer(10000, 1000, compteurGen, rep, message);
        timerProp = new myCountDownTimer(10000, 1000, compteurProp, valider, null);
        
		timerGen.start();
        
        rep.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		//repondre()
        		s = compteurGen.getText().toString();
        		timerGen.cancel();
        		prop.setVisibility(View.VISIBLE);
        		valider.setVisibility(View.VISIBLE);
        		compteurProp.setVisibility(View.VISIBLE);
        		timerProp.start();
        	}
        });
        
        valider.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		//validerReponse()
        		int i = Integer.parseInt(s);
        		timerProp.cancel();
        		prop.setVisibility(View.INVISIBLE);
        		valider.setVisibility(View.INVISIBLE);
        		compteurProp.setVisibility(View.INVISIBLE);
        		
        		if((prop.getText().toString()).equals(reponse) ) {
        			message.setText("BRAVO !");
        			rep.setClickable(false);
        			valider.setClickable(false);
        		}
        		else {
        			message.setText("FAUX !");
            		timerGen = new myCountDownTimer(i*1000, 1000, compteurGen, rep, message);
            		timerGen.start();
        		}
        	}
        });
        
        arreter.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		//arreterTimers()
        		timerProp.cancel();
        		timerGen.cancel();
        	}
        });
        
        compteurProp.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (v.getText().toString().equals("Time out!")) {
					//propTimeOut();
					int i = Integer.parseInt(s);
					timerProp.cancel();
					prop.setVisibility(View.INVISIBLE);
	        		valider.setVisibility(View.INVISIBLE);
	        		compteurProp.setVisibility(View.INVISIBLE);
            		timerGen = new myCountDownTimer(i*1000, 1000, compteurGen, rep, message);
            		timerGen.start();
				}
				return false;
			}
        });
        
    }
}
    